﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataObject.DataAccess;
using DataObject.Database;
using System.Web.Security;

public partial class Pages_Boat_BoatItem : System.Web.UI.Page
{
    public string BoatId { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["BoatId"] != null)
        {
            BoatId = Request["BoatId"];
        }
        else BoatId = string.Empty;

        if (!IsPostBack)
        {
            LoadData();
        }
    }
    void LoadData()
    {
        BoatEntity item = BoatDAO.CreateInstant().GetBoat(BoatId);
        if (item != null)
        {
            txtBoatId.Text = item.BoatID;
            txtBoatName.Text = item.BoatName;
            txtNumOfCabins.Text = item.NumOfCabins.ToString();
            txtNumOfFloors.Text = item.NumOfFloors.ToString();
            cbActive.Checked = item.Active;
            txtColor.Value = item.Color;
        }
    }
    BoatEntity GetBoatEntity()
    {
        BoatEntity item = BoatDAO.CreateInstant().GetBoat(BoatId);
        if (item == null)
        {
            item = new BoatEntity();
            //item.BoatID = "Boats/" + DateTime.Now.ToString("ddhhmmss");
            item.BoatID = txtBoatId.Text;

        }
        item.BoatName = txtBoatName.Text;
        item.NumOfCabins = int.Parse(txtNumOfCabins.Text);
        item.NumOfFloors = int.Parse(txtNumOfFloors.Text);
        item.Active = cbActive.Checked;
        item.Color = txtColor.Value;
        return item;
    }
    public bool CheckBoatExisted()
    {
        BoatEntity _boatEntity = BoatDAO.CreateInstant().GetBoat(txtBoatId.Text);
        return (null != _boatEntity);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (CheckBoatExisted() && BoatId.Equals(string.Empty))
        {
            lblAlert.Visible = true;
        }
        else
        {
            lblAlert.Visible = false;

            BoatEntity item = GetBoatEntity();
            if (!BoatId.Equals(string.Empty))
            {
                BoatDAO.CreateInstant().UpdateBoat(item);
                Response.Redirect("BoatList.aspx");
            }
            else
            {
                BoatDAO.CreateInstant().Add(item);
                Response.Redirect("CabinList.aspx?BoatId=" + item.BoatID);
            }
        }
    }

}
